package com.xsy.android.compose.wandroid.ui.main

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.google.gson.Gson
import com.xsy.android.compose.wandroid.logic.model.ArticleModel
import com.xsy.android.compose.wandroid.logic.utils.getHtmlText
import com.xsy.android.compose.wandroid.ui.page.artice.ArticlePage
import com.xsy.android.compose.wandroid.ui.page.login.LoginPage
import com.xsy.android.compose.wandroid.ui.page.login.LoginViewModel
import java.net.URLEncoder

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
object Routes {
    const val HOME_ROUTE = "home_page"
    const val ARTICLE_ROUTE = "article_page"
    const val ARTICLE_ROUTE_URL = "article_route_url"
    const val LOGIN_ROUTE = "login_page"
}


@Composable
fun NavGraph(modifier: Modifier = Modifier) {
    val navController = rememberNavController()
    val actions = remember(navController) { NavigationUtil(navController) }
    NavHost(navController = navController, startDestination = Routes.HOME_ROUTE) {
        composable(Routes.HOME_ROUTE) {
            val viewModel: HomeViewModel = viewModel()
            val position by viewModel.position.observeAsState()
            MainPage(current = position ?: 0, actions = actions) { tab ->
                viewModel.onPositionChange(tab)
            }
        }
        composable(Routes.LOGIN_ROUTE) {
            val viewModel:LoginViewModel = viewModel()
            val loginState by viewModel.state.observeAsState()
            LoginPage(onNavigationEvent = actions, loginState = loginState, toLogout = {
                viewModel.logout()
            }, toLoginOrRegister = {
                viewModel.toLoginOrRegister(it)
            } )
        }
        composable(
            "${Routes.ARTICLE_ROUTE}/{${Routes.ARTICLE_ROUTE_URL}}",
            arguments = listOf(navArgument(Routes.ARTICLE_ROUTE_URL) {
                type = NavType.StringType
            })
        ) { backStackEntry ->
            val arguments = requireNotNull(backStackEntry.arguments)
            val parcelable = arguments.getString(Routes.ARTICLE_ROUTE_URL)
            val fromJson = Gson().fromJson(parcelable, ArticleModel::class.java)
            ArticlePage(article = fromJson, onBack = actions.upPress)
        }
    }
}

class NavigationUtil(navController: NavHostController) {
    val enterArticle: (ArticleModel) -> Unit = { article ->
        article.desc = ""
        article.title = getHtmlText(article.title)
        val gson = Gson().toJson(article).trim()
        val result = URLEncoder.encode(gson, "utf-8")
        navController.navigate("${Routes.ARTICLE_ROUTE}/$result")
    }
    val login: () -> Unit = {
        navController.navigate(Routes.LOGIN_ROUTE)
    }

    val upPress: () -> Unit = {
        navController.navigateUp()
    }
}